page.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. "use client";
  2. import { getFindPwdApi } from "@/api/user";
  3. import ButtonOwn from "@/components/ButtonOwn";
  4. import DomainFooter from "@/components/DomainFooter";
  5. import HeaderBack from "@/components/HeaderBack";
  6. import { useLogout } from "@/hooks/useLogout";
  7. import { Form, Input, Toast } from "antd-mobile";
  8. import { FormInstance } from "antd-mobile/es/components/form";
  9. import clsx from "clsx";
  10. import { useTranslations } from "next-intl";
  11. import { useSearchParams } from "next/navigation";
  12. import { FC, useRef, useState } from "react";
  13. import { Simulate } from "react-dom/test-utils";
  14. import "./page.scss";
  15. import change = Simulate.change;
  16. interface Props {}
  17. const ResetPhone: FC<Props> = () => {
  18. const t = useTranslations();
  19. const { logout } = useLogout();
  20. let searchParams = useSearchParams();
  21. let user_phone = searchParams.get("userPhone");
  22. let code = searchParams.get("code");
  23. const findPwdRequest = ({ pwd }: { pwd: string }) => {
  24. getFindPwdApi({ user_phone, code, pwd })
  25. .then(async (res) => {
  26. if (res.code == 200) {
  27. Toast.show({ icon: "success", content: t("code.200"), maskClickable: false });
  28. await logout();
  29. }
  30. })
  31. .catch((error) => {
  32. Toast.show(t(`code.${error.data.code}`));
  33. });
  34. };
  35. /**
  36. * @description
  37. */
  38. const [visible, setVisible] = useState(false);
  39. const spanClassName = clsx("iconfont", {
  40. "icon-kejian": visible,
  41. "icon-bukejian": !visible,
  42. });
  43. const formRef = useRef<FormInstance>(null);
  44. const onFinish = (values: { pwd: string; checkPwd: string }) => {
  45. findPwdRequest(values);
  46. };
  47. const onValuesChange = (changeValue: { pwd: string }) => {
  48. if (changeValue.pwd) {
  49. formRef.current?.setFieldValue("pwd", changeValue.pwd.replace(/[^\w\.\/]/gi, ""));
  50. }
  51. };
  52. const checkValidator = (rules: any, value: string) => {
  53. const password = formRef.current?.getFieldValue("pwd");
  54. if (value !== password) {
  55. return Promise.reject(new Error(t("form.checkPwdReg")));
  56. }
  57. return Promise.resolve();
  58. };
  59. return (
  60. <div className="confirmPassword-box">
  61. <HeaderBack />
  62. <div className="main custom-form">
  63. <div className="title">
  64. <h2>{t("confirmPwdPage.title")}</h2>
  65. {/* <div>修改密码 {user_phone}</div> */}
  66. </div>
  67. <Form
  68. style={{
  69. "--border-bottom": "none",
  70. "--border-top": "none",
  71. "--border-inner": "none",
  72. }}
  73. onFinish={onFinish}
  74. ref={formRef}
  75. onValuesChange={onValuesChange}
  76. footer={<ButtonOwn active>{t("confirmPwdPage.complete")}</ButtonOwn>}
  77. >
  78. <Form.Item
  79. name="pwd"
  80. label=""
  81. extra={
  82. <span
  83. className={spanClassName}
  84. onClick={() => setVisible(!visible)}
  85. ></span>
  86. }
  87. rules={[
  88. { required: true, message: t("form.passwordReg") },
  89. { min: 6, max: 20, message: t("form.passwordMinReg") },
  90. ]}
  91. >
  92. <Input
  93. placeholder={t("form.newPwd")}
  94. maxLength={20}
  95. type={visible ? "text" : "password"}
  96. />
  97. </Form.Item>
  98. <Form.Item
  99. name="checkPwd"
  100. label=""
  101. rules={[
  102. {
  103. min: 6,
  104. max: 20,
  105. validator: checkValidator,
  106. },
  107. ]}
  108. >
  109. <Input placeholder={t("form.checkPwd")} maxLength={20} type={"password"} />
  110. </Form.Item>
  111. </Form>
  112. </div>
  113. <DomainFooter />
  114. </div>
  115. );
  116. };
  117. export default ResetPhone;